package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomLogin;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.adsrt.lib.Field;
import br.com.telescope.adsrt.lib.Form;
import br.com.telescope.sql.ModelQuery;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.InsertFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.LookupFeature;
import br.com.telescope.t2.feature.ViewFeature;
import br.com.telescope.util.ObjectUtil;
import java.util.ArrayList;
import java.util.List;
import solutions.ferpa.ztest.ent.*;

/**
 * Usuários que exercem a função.
 * <p>
 * Permite consultar/atualizar os usuários que exerce uma função.
 * <P>
 * Telescope origin: RTSYSINF.CAD_FUNCAO_USUARIOS
 * <P>
 * Base entity: RTAUTH.FUNCOES_USUARIOS
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadFuncaoUsuarios extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTSYSINF.CAD_FUNCAO_USUARIOS";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Usuários que exercem a função");
    }
    
    protected EntFuncoesUsuarios base;
    protected EntUsuarios usua;
    protected EntFuncoesUsuarios rec; // NEED_TJW1_COMPATIBILITY
    
    // Service parameters
    private DomId pFuncaoId;
    
    // Record items
    private DomId aUsuarioId;
    private DomId aFuncaoId;
    private DomLogin aLogin;
    private DomId aId;
    
    // Query items
    
    @Override
    public String getBaseSql() {
        return "select BASE.*"
                + " , USUA.LOGIN as LOGIN"
                + " , USUA.ID as Z_USUA_ID"
                + " from FUNCOES_USUARIOS BASE"
                + " join USUARIOS USUA on USUA.ID = BASE.USUARIO_ID";
    }
    
    @Override
    public void initAttributes() {
        
        aUsuarioId = new DomId();
        aUsuarioId.context(ctx).name("USUARIO_ID").label("Usuário");
        
        aFuncaoId = new DomId();
        aFuncaoId.context(ctx).name("FUNCAO_ID").label("Função");
        
        aLogin = new DomLogin();
        aLogin.context(ctx).name("LOGIN").label("Login");
        aLogin.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT LOGIN as valor"
                + " , LOGIN as pesquisar"
                + " , LOGIN as descricao"
                + " from USUARIOS USUA)", "q"));
        aLogin.setAutocompleteLength(1);
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
    }
    
    @Override
    public void initParameters() {
        pFuncaoId = new DomId(ctx, "FUNCAO_ID", "Função");
        pFuncaoId.setValue(ctx.getParameter("P_FUNCAO_ID"));
        if (pFuncaoId.isNotNull()) {
            aFuncaoId.setValue(pFuncaoId.getValue());
        }
    }
    
    @Override
    public List<Field> getParameters() {
        if (allParameters == null) {
            allParameters = new ArrayList<Field>();
            allParameters.add(pFuncaoId);
        }
        return allParameters;
    }
    
    @Override
    public void copyBindParameters() {
        EntFuncoesUsuarios record = getRecord();
        if (pFuncaoId.isNotNull()) {
            record.setFuncaoId(pFuncaoId.getValue());
        }
    }
    
    @Override
    public void retrieveBindParameters() {
        pFuncaoId.setValue(getRecord().valueOfFuncaoId());
    }
    
    @Override
    public String getQueryPkColumn() {
        return "BASE.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntFuncoesUsuarios getRecord() {
        if (base == null) {
            base = new EntFuncoesUsuarios(ctx);
            rec = base;
        }
        return this.base;
    }
    
    @Override
    public String getContextName() {
        return aLogin.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "LOGIN";
    }
    
    @Override
    public void translateInsertFks(Form form) {
        ModelQuery query;
        
        // USUARIO_ID
        try {
            if (form.hasFields("LOGIN")) {
                query = createQuery()
                        .select("USUA.ID")
                        .from("USUARIOS", "USUA")
                        .filter("USUA.LOGIN", form.get("LOGIN"))
                        ;
                base.setUsuarioId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    @Override
    public void translateUpdateFks(Form form) {
        ModelQuery query;
        
        // USUARIO_ID
        try {
            if (form.hasFields("LOGIN")) {
                query = createQuery()
                        .select("USUA.ID")
                        .from("USUARIOS", "USUA")
                        .filter("USUA.LOGIN", form.get("LOGIN"))
                        ;
                base.setUsuarioId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            addField(aLogin, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends InsertFeature {
        
        @Override
        public void initForm() {
            aLogin.setOptional(false);
            addField(aLogin, "rqd");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aLogin, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="VIEW">
    public class FView extends ViewFeature {
        
        @Override
        public void initForm() {
            addField(aLogin, "view");
        }
        
        {
            setFeatureName("VIEW");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LOGIN_LOOKUP">
    public class FLoginLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aLogin.setEditable(true);
            addField(aLogin, "opt");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("USUA.LOGIN as LOGIN")
                    .from("USUARIOS", "USUA")
                    ;
        }
        
        {
            setFeatureName("LOGIN_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FList());
        addFeature(new FInsert());
        addFeature(new FDelete());
        addFeature(new FView());
        addFeature(new FLoginLookup());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "LIST";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public Long getUsuarioId() {
        return aUsuarioId.getValue();
    }
    
    public void setUsuarioId(Long value) {
        this.aUsuarioId.setValue(value);
    }
    
    public Long getFuncaoId() {
        return aFuncaoId.getValue();
    }
    
    public void setFuncaoId(Long value) {
        this.aFuncaoId.setValue(value);
    }
    
    public String getLogin() {
        return aLogin.getValue();
    }
    
    public void setLogin(String value) {
        this.aLogin.setValue(value);
    }
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public Long getParameterFuncaoId() {
        return pFuncaoId.getValue();
    }
    
    public void setParameterFuncaoId(Long value) {
        pFuncaoId.setValue(value);
        aFuncaoId.setValue(value);
    }
    // </editor-fold>
    
    
    @Override
    public String getJsServiceFile() {
        return "ui/rtsysinf/cad_funcao_usuarios.js";
    }
}
